package com.zhengtoudai.ui.presenter;


import android.app.Activity;

import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.PerActivity;
import com.zhengtoudai.ui.bean.BorrowDetails;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.view.BorrowDetailView;
import com.zhengtoudai.util.AbToastUtil;

import javax.inject.Inject;

import rx.Observer;
import rx.functions.Action0;

/**
 * Created by Administrator on 2016/9/1 0001.
 */

@PerActivity
public class BorrowDetailPresenterImpl extends BasePresenterImpl<BorrowDetailView> implements BorrowDetailPresenter {
    private Activity mContext;

    @Inject
    public BorrowDetailPresenterImpl(Activity context){
        mContext=context;
    }

    @Override
    public void borrowDetail(String borrowEId) {
        mSubscription=HttpApi.getInstance().borrowDetail(borrowEId)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.showDetailDialog();
                    }
                }).subscribe(new Observer<Result<BorrowDetails>>() {
            @Override
            public void onCompleted() {
                mView.dismissDetailDialog();
            }

            @Override
            public void onError(Throwable e) {
                HttpUtil.disposeOnFailure(mContext,e);
                mView.dismissDetailDialog();
            }

            @Override
            public void onNext(Result<BorrowDetails> result) {
                if (result.getCode()== Config.SUCCESSFUL_CODE){
                    mView.onDetailResult(result.getResult());
                }else{
                    AbToastUtil.showToast(mContext,result.getMessage());
                }
            }
        });
    }
}
